import {
  request
} from '../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // picDomain: config.picDomain,
    carlist: [],
    finalMoney: 0,
    totalMoney: 0,
    subtractMoney: 0,
    allChecked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getlist();
  },

  getlist() {
    wx.showLoading();

    request({
      url: "/p/shopCart/info",
      header: {
        'Authorization': "7Jr/9m22Ty+r96r/kyX+ejusJzlakoKY6ZqVdAtPBBOFdSG/emDQUuO7h8151+4/"
      },
      method: "POST",
      data: {},

    }).then(res => {

      console.log(998);
      if (res.length > 0) {
        var carlist = res[0].shopCartItemDiscounts;
        carlist.forEach(shopCartItemDiscount => {
          shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
            shopCartItem.checked = false;
          });
        });
        this.setData({
          carlist: carlist,
          allChecked: false
        });
      } else {
        this.setData({
          carlist: []
        });
      }

      this.setprice();

      wx.hideLoading();

    })
    //   http.request(params);
  },

  /**
   * 去结算
   */
  toFirmOrder: function () {
    var carlist = this.data.carlist;
    console.log(carlist)
    var basketIds = [];
    carlist.forEach(shopCartItemDiscount => {
      shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
        if (shopCartItem.checked) {
          basketIds.push(shopCartItem.basketId)
        }
      })
    })
    if (!basketIds.length) {
      wx.showToast({
        title: '请选择商品',
        icon: "none"
      })
      return
    }
    wx.setStorageSync("basketIds", JSON.stringify(basketIds));
    wx.navigateTo({
      // url: '/pages/submit-order/submit-order?orderEntry=0',
      url: '/pages/creatOrder/creatOrder?orderEntry=0'
    })
  },

  /**
   * 全选
   */
  onSelAll: function () {
    var allChecked = this.data.allChecked;
    allChecked = !allChecked; //改变状态
    var carlist = this.data.carlist;

    for (var i = 0; i < carlist.length; i++) {
      var cItems = carlist[i].shopCartItems;
      for (var j = 0; j < cItems.length; j++) {
        cItems[j].checked = allChecked;
      }
    }

    this.setData({
      allChecked: allChecked,
      carlist: carlist
    });
    this.setprice(); //计算总价
  },

  /**
   * 每一项的选择事件
   */
  onSelectedItem: function (e) {
    var index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    var scindex = e.currentTarget.dataset.scindex;

    var carlist = this.data.carlist; // 获取购物车列表
    var checked = carlist[scindex].shopCartItems[index].checked; // 获取当前商品的选中状态
    carlist[scindex].shopCartItems[index].checked = !checked; // 改变状态
    this.setData({
      carlist: carlist
    });
    this.checkAllSelected(); //检查全选状态
    this.setprice(); //计算总价
  },

  /**
   * 检查全选状态
   */
  checkAllSelected: function () {
    var allChecked = true;
    var carlist = this.data.carlist;

    var flag = false;
    for (var i = 0; i < carlist.length; i++) {
      var cItems = carlist[i].shopCartItems;
      for (var j = 0; j < cItems.length; j++) {
        if (!cItems[j].checked) {
          allChecked = !allChecked;
          flag = true;
          break;
        }
      }
      if (flag) {
        break;
      }
    }
    this.setData({
      allChecked: allChecked
    });
  },

  /**
   * 计算购物车总额
   */
  setprice: function () {
    var carlist = this.data.carlist;
    var shopCartIds = [];
    for (var i = 0; i < carlist.length; i++) {
      var cItems = carlist[i].shopCartItems;
      for (var j = 0; j < cItems.length; j++) {
        if (cItems[j].checked) {
          shopCartIds.push(cItems[j].basketId);
        }
      }
    }
    console.log(shopCartIds, 'shopCartIds');
    var this_ = this;
    wx.showLoading();
    request({
      url: "/p/shopCart/totalPay",
      header: {
        'Authorization': "7Jr/9m22Ty+r96r/kyX+ejusJzlakoKY6ZqVdAtPBBOFdSG/emDQUuO7h8151+4/"
      },
      method: "POST",
      data: shopCartIds
    }).then(res => {
      console.log(res, 'res');
      this_.setData({
        finalMoney: res.finalMoney,
        totalMoney: res.totalMoney,
        subtractMoney: res.subtractMoney
      });
      wx.hideLoading();
    })
    //   http.request(params);

  },

  /**
   * 减少数量
   */
  onCountMinus: function (e) {
    var index = e.currentTarget.dataset.index;
    var scindex = e.currentTarget.dataset.scindex;
    var carlist = this.data.carlist;
    var prodCount = carlist[scindex].shopCartItems[index].prodCount;
    if (prodCount > 1) {
      this.updateCount(carlist, scindex, index, -1);
    }
  },

  /**
   * 增加数量
   */
  onCountPlus: function (e) {
    var index = e.currentTarget.dataset.index;
    var scindex = e.currentTarget.dataset.scindex;
    var carlist = this.data.carlist;
    this.updateCount(carlist, scindex, index, 1);
  },


  /**
   * 改变购物车数量接口
   */
  updateCount: function (carlist, scindex, index, prodCount) {
    var this_ = this;
    wx.showLoading({
      mask: true
    });
    request({
      url: "/p/shopCart/changeItem",
      header: {
        'Authorization': "7Jr/9m22Ty+r96r/kyX+ejusJzlakoKY6ZqVdAtPBBOFdSG/emDQUuO7h8151+4/"
      },
      method: "POST",
      data: {
        count: prodCount,
        prodId: carlist[scindex].shopCartItems[index].prodId,
        skuId: carlist[scindex].shopCartItems[index].skuId,
        shopId: 1
      }

    }).then(res => {
      carlist[scindex].shopCartItems[index].prodCount += prodCount;
      this_.setData({
        carlist: carlist
      });
      this_.setprice();
      wx.hideLoading();
    })
  },

  // 删除
  del: function () {
    var this_ = this;

    var carlist = this.data.carlist;
    var basketIds = [];
    for (var i = 0; i < carlist.length; i++) {
      var cItems = carlist[i].shopCartItems;
      for (var j = 0; j < cItems.length; j++) {
        if (cItems[j].checked) {
          basketIds.push(cItems[j].basketId);
        }
      }
    }

    if (basketIds.length == 0) {
      wx.showToast({
        title: '请选择商品',
        icon: "none"
      })
    } else {
      request({
        url: "/p/shopCart/deleteItem",
        header: {
          'Authorization': "7Jr/9m22Ty+r96r/kyX+ejusJzlakoKY6ZqVdAtPBBOFdSG/emDQUuO7h8151+4/"
        },
        method: "DELETE",
        data: basketIds
      }).then(res => {
        wx.hideLoading();
        this_.getlist();
      })
    }


  }


})